package renderer2;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.lwjgl.opengl.GL11;

public abstract class GenericRenderer <T>{
	private static final Map<Integer, Integer> displayListMap = new HashMap<Integer, Integer>();
	private static final List<Integer> textureList = new ArrayList<Integer>();

	public static synchronized int generateDisplayLists(int par0) {
		int j = GL11.glGenLists(par0);
		displayListMap.put(Integer.valueOf(j), Integer.valueOf(par0));
		return j;
	}

	public static synchronized void deleteDisplayLists(int par0) {
		GL11.glDeleteLists(par0, (displayListMap.remove(Integer.valueOf(par0))).intValue());
	}

	public static synchronized void deleteTexturesAndDisplayLists() {
		deleteAllDisplayLists();
		deleteAllTextures();
	}

	public static synchronized ByteBuffer createDirectByteBuffer(int par0) {
		return ByteBuffer.allocateDirect(par0).order(ByteOrder.nativeOrder());
	}

	public static IntBuffer createDirectIntBuffer(int par0) {
		return createDirectByteBuffer(par0 << 2).asIntBuffer();
	}

	public static FloatBuffer createDirectFloatBuffer(int par0) {
		return createDirectByteBuffer(par0 << 2).asFloatBuffer();
	}
	
	public abstract void render(T t);

	/**/

	private static synchronized void deleteAllTextures() {
		for (int i = 0; i < textureList.size(); ++i) {
			GL11.glDeleteTextures((textureList.get(i)).intValue());
		}

		textureList.clear();
	}

	private static synchronized void deleteAllDisplayLists() {
		Iterator<Entry<Integer, Integer>> iterator = displayListMap.entrySet().iterator();

		while (iterator.hasNext()) {
			Entry<Integer, Integer> entry = iterator.next();
			GL11.glDeleteLists(((Integer) entry.getKey()).intValue(), ((Integer) entry.getValue()).intValue());
		}

		displayListMap.clear();
	}
}
